package com.raidpoints.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ChatServ extends HttpServlet{
	
	ArrayList<String> msgq;
	
	public void init(){
		msgq = null;
		msgq = (ArrayList<String>) this.getServletContext().getAttribute("chatq");
	}
	
	public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
		if(msgq == null) msgq = (ArrayList<String>) this.getServletContext().getAttribute("chatq");
		
		res.setCharacterEncoding("UTF-8");
		res.setContentType("text/event-stream");
		PrintWriter writer = res.getWriter();
		
//		if(msgq != null){
//		Iterator<String> it = msgq.iterator();
//	
//		while(it.hasNext()){
//			String s = it.next();
//			//System.out.println(s);
//			writer.write("data: " + s + "\n");
//			it.remove();
//		}
//		}
		Random rnd = new Random();
		writer.write("data: hey " + rnd.nextInt() + "\n\n");
		
		writer.flush();
		writer.close();
	}
}
